home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / chunk.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  6KB  |  201 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Simple class to read IFF chunks.
  5.  
  6. An IFF chunk (used in formats such as AIFF, TIFF, RMFF (RealMedia File
  7. Format)) has the following structure:
  8.  
  9. +----------------+
  10. | ID (4 bytes)   |
  11. +----------------+
  12. | size (4 bytes) |
  13. +----------------+
  14. | data           |
  15. | ...            |
  16. +----------------+
  17.  
  18. The ID is a 4-byte string which identifies the type of chunk.
  19.  
  20. The size field (a 32-bit value, encoded using big-endian byte order)
  21. gives the size of the whole chunk, including the 8-byte header.
  22.  
  23. Usually an IFF-type file consists of one or more chunks.  The proposed
  24. usage of the Chunk class defined here is to instantiate an instance at
  25. the start of each chunk and read from the instance until it reaches
  26. the end, after which a new instance can be instantiated.  At the end
  27. of the file, creating a new instance will fail with a EOFError
  28. exception.
  29.  
  30. Usage:
  31. while True:
  32.     try:
  33.         chunk = Chunk(file)
  34.     except EOFError:
  35.         break
  36.     chunktype = chunk.getname()
  37.     while True:
  38.         data = chunk.read(nbytes)
  39.         if not data:
  40.             pass
  41.         # do something with data
  42.  
  43. The interface is file-like.  The implemented methods are:
  44. read, close, seek, tell, isatty.
  45. Extra methods are: skip() (called by close, skips to the end of the chunk),
  46. getname() (returns the name (ID) of the chunk)
  47.  
  48. The __init__ method has one required argument, a file-like object
  49. (including a chunk instance), and one optional argument, a flag which
  50. specifies whether or not chunks are aligned on 2-byte boundaries.  The
  51. default is 1, i.e. aligned.
  52. '''
  53.  
  54. class Chunk:
  55.     
  56.     def __init__(self, file, align = True, bigendian = True, inclheader = False):
  57.         import struct as struct
  58.         self.closed = False
  59.         self.align = align
  60.         if bigendian:
  61.             strflag = '>'
  62.         else:
  63.             strflag = '<'
  64.         self.file = file
  65.         self.chunkname = file.read(4)
  66.         if len(self.chunkname) < 4:
  67.             raise EOFError
  68.         len(self.chunkname) < 4
  69.         
  70.         try:
  71.             self.chunksize = struct.unpack(strflag + 'L', file.read(4))[0]
  72.         except struct.error:
  73.             raise EOFError
  74.  
  75.         if inclheader:
  76.             self.chunksize = self.chunksize - 8
  77.         
  78.         self.size_read = 0
  79.         
  80.         try:
  81.             self.offset = self.file.tell()
  82.         except (AttributeError, IOError):
  83.             self.seekable = False
  84.  
  85.         self.seekable = True
  86.  
  87.     
  88.     def getname(self):
  89.         '''Return the name (ID) of the current chunk.'''
  90.         return self.chunkname
  91.  
  92.     
  93.     def getsize(self):
  94.         '''Return the size of the current chunk.'''
  95.         return self.chunksize
  96.  
  97.     
  98.     def close(self):
  99.         if not self.closed:
  100.             self.skip()
  101.             self.closed = True
  102.         
  103.  
  104.     
  105.     def isatty(self):
  106.         if self.closed:
  107.             raise ValueError, 'I/O operation on closed file'
  108.         self.closed
  109.         return False
  110.  
  111.     
  112.     def seek(self, pos, whence = 0):
  113.         '''Seek to specified position into the chunk.
  114.         Default position is 0 (start of chunk).
  115.         If the file is not seekable, this will result in an error.
  116.         '''
  117.         if self.closed:
  118.             raise ValueError, 'I/O operation on closed file'
  119.         self.closed
  120.         if not self.seekable:
  121.             raise IOError, 'cannot seek'
  122.         self.seekable
  123.         if whence == 1:
  124.             pos = pos + self.size_read
  125.         elif whence == 2:
  126.             pos = pos + self.chunksize
  127.         
  128.         if pos < 0 or pos > self.chunksize:
  129.             raise RuntimeError
  130.         pos > self.chunksize
  131.         self.file.seek(self.offset + pos, 0)
  132.         self.size_read = pos
  133.  
  134.     
  135.     def tell(self):
  136.         if self.closed:
  137.             raise ValueError, 'I/O operation on closed file'
  138.         self.closed
  139.         return self.size_read
  140.  
  141.     
  142.     def read(self, size = -1):
  143.         '''Read at most size bytes from the chunk.
  144.         If size is omitted or negative, read until the end
  145.         of the chunk.
  146.         '''
  147.         if self.closed:
  148.             raise ValueError, 'I/O operation on closed file'
  149.         self.closed
  150.         if self.size_read >= self.chunksize:
  151.             return ''
  152.         if size < 0:
  153.             size = self.chunksize - self.size_read
  154.         
  155.         if size > self.chunksize - self.size_read:
  156.             size = self.chunksize - self.size_read
  157.         
  158.         data = self.file.read(size)
  159.         self.size_read = self.size_read + len(data)
  160.         if self.size_read == self.chunksize and self.align and self.chunksize & 1:
  161.             dummy = self.file.read(1)
  162.             self.size_read = self.size_read + len(dummy)
  163.         
  164.         return data
  165.  
  166.     
  167.     def skip(self):
  168.         '''Skip the rest of the chunk.
  169.         If you are not interested in the contents of the chunk,
  170.         this method should be called so that the file points to
  171.         the start of the next chunk.
  172.         '''
  173.         if self.closed:
  174.             raise ValueError, 'I/O operation on closed file'
  175.         self.closed
  176.         if self.seekable:
  177.             
  178.             try:
  179.                 n = self.chunksize - self.size_read
  180.                 if self.align and self.chunksize & 1:
  181.                     n = n + 1
  182.                 
  183.                 self.file.seek(n, 1)
  184.                 self.size_read = self.size_read + n
  185.                 return None
  186.             except IOError:
  187.                 pass
  188.             except:
  189.                 None<EXCEPTION MATCH>IOError
  190.             
  191.  
  192.         None<EXCEPTION MATCH>IOError
  193.         while self.size_read < self.chunksize:
  194.             n = min(8192, self.chunksize - self.size_read)
  195.             dummy = self.read(n)
  196.             if not dummy:
  197.                 raise EOFError
  198.             dummy
  199.  
  200.  
  201.